<?php

namespace app\index\service;

use app\common\model\RankBroadcast;
use app\index\dao\RankBroadcastDao;
use app\index\model\UM as UM;
use think\Collection;

/**
 * Class BroadcastService
 * @package app\index\service
 */
class BroadcastService
{
    // dao
    private RankBroadcastDao $rankBroadcastDao;

    public function __construct(RankBroadcastDao $rankBroadcastDao)
    {
        $this->rankBroadcastDao = $rankBroadcastDao;
    }

    /**
     * 列表
     * @param UM|null $user
     * @param int|null $page
     * @param int|null $page_size
     * @return Collection
     */
    public function index(?UM $user, $type, ?int $page = null, ?int $page_size = null): Collection
    {
        return $this->rankBroadcastDao->select($type, $page, $page_size);
    }

    /**
     * 新增播报
     * @param $type
     * @param $memberId
     * @param $title
     * @param array $messages [{red,text,type}]
     * @return RankBroadcast
     */
    public function addBroadcast($type, $memberId, $title, array $messages): RankBroadcast
    {
        $params = [
            'type' => $type,
            'memberId' => $memberId,
            'title' => $title,
            'content' => $messages,
        ];

        return $this->rankBroadcastDao->insertActive($params, []);
    }


}
